//
//  GetBackViewController.m
//  projectOfTeamSample
//
//  Created by lanou3g on 15/11/21.
//  Copyright © 2015年 liukai. All rights reserved.
//

#import "GetBackViewController.h"
#import "GetBackView.h"
#import <AVUser.h>
#import "loginViewController.h"
@interface GetBackViewController ()
@property (nonatomic,strong)GetBackView *getview;
@end

@implementation GetBackViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.getview = [[GetBackView alloc]initWithFrame:self.view.frame];
    [self.getview.getButton addTarget:self action:@selector(didGetBackButtonAction:) forControlEvents:(UIControlEventTouchUpInside)];
    self.view.backgroundColor = [UIColor whiteColor];
     self.view = self.getview;
}

-(void)didGetBackButtonAction:(UIButton *)send{
    
        [AVUser requestPasswordResetWithPhoneNumber:self.getview.getText.text block:^(BOOL succeeded, NSError *error) {
            if (succeeded) {
                [self.getview setGetWithPhone];
                [self.getview.phoneButton addTarget:self action:@selector(touchPhoneButtonAction:) forControlEvents:(UIControlEventTouchUpInside)];
                self.view = self.getview;
                [self.getview.getButton addTarget:self action:@selector(touchPhoneButtonAction:) forControlEvents:(UIControlEventTouchUpInside)];
                return ;
            }else{
                
                [AVUser requestPasswordResetForEmailInBackground:self.getview.getText.text block:^(BOOL succeeded, NSError *error) {
                    if (succeeded) {
                        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"验证链接已发送,请到邮箱中验证" message:nil preferredStyle:(UIAlertControllerStyleActionSheet)];
                        UIAlertAction *action = [UIAlertAction actionWithTitle:@"好的" style:(UIAlertActionStyleDefault) handler:nil];
                        [alert addAction:action];
                        [self presentViewController:alert animated:YES completion:nil];
                    }else{
                        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"此邮箱未认证,请认证" message:nil preferredStyle:(UIAlertControllerStyleActionSheet)];
                        UIAlertAction *action = [UIAlertAction actionWithTitle:@"OK" style:(UIAlertActionStyleDefault) handler:nil];
                        [alert addAction:action];
                        [self presentViewController:alert animated:YES completion:nil];
                    }
                }];
            }
        }];
        
    
}

-(void)touchPhoneButtonAction:(UIButton *)send{
    [AVUser resetPasswordWithSmsCode:self.getview.phoneText.text newPassword:self.getview.phoneNewPasswordText.text block:^(BOOL succeeded, NSError *error) {
        if (succeeded) {
            [self.navigationController popViewControllerAnimated:YES];
        }
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
